package com.web.oa.controller;

import cn.hutool.http.HttpResponse;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import com.web.oa.pojo.ActiveUser;
import com.web.oa.pojo.BaoxiaoBill;
import com.web.oa.pojo.Employee;
import com.web.oa.service.BaoxiaoService;
import com.web.oa.service.WorkFlowService;
import com.web.oa.utils.Constants;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

/**流程*/
@Controller
public class WorkFlowController {
    @Autowired
    private WorkFlowService workFlowService;

    /**部署流程*/
    @RequestMapping("/deployProcess")
    public String deployProcess(MultipartFile fileName, String processName) throws IOException {
        //用于上传的流程文件部署到工作流引擎中，并设置新流程定义的名称
        this.workFlowService.addFlowProcess(fileName.getInputStream(),processName);
        //返回到add_process.jsp发布流程这个视图页面中
        return "add_process";
    }

    /**对应着流程管理中的查看流程*/
    @RequestMapping("/processDefinitionList")
    public String processDefinitionList(Model model){
        //查询部署流程对象信息
        List<Deployment> depList=workFlowService.findDeploymentList();
        //查询流程定义信息
        List<ProcessDefinition> pdList=workFlowService.findProcessDefinitionList();
        model.addAttribute("depList",depList);
        model.addAttribute("pdList",pdList);
        return "workflow_list";
    }

    /**设置待办任务*/
    @RequestMapping("/myTaskList")
    public String myTaskList(HttpSession session, Model model){
        //从ActiveUser中获取
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        List<Task> taskList=this.workFlowService.findMyTaskList(activeUser.getUsername());
        //将查询到的任务列表存储到Model中,以便前端页面展示
        model.addAttribute("taskList",taskList);
        //最后返回workflow_task.jsp页面
        return "workflow_task";
    }

    /**报销流程*/
    @RequestMapping("/saveStartBaoxiao")
    public String saveStartBaoxiao(String id,String title,String money,String remark){
        //从Session中获取当前登录用户对象
        ActiveUser activeUser= (ActiveUser) SecurityUtils.getSubject().getPrincipal();

        BaoxiaoBill baoxiaoBill=new BaoxiaoBill(Double.valueOf(money),title,remark,1,activeUser.getId());

        if(id!=null && !"".equals(id)){
            baoxiaoBill.setId(Long.valueOf(id));
            workFlowService.saveBaoxiaoBill(baoxiaoBill);
        }else{
            workFlowService.saveBaoxiaoBill(baoxiaoBill);
            //第二步：启动流程
            //调用该方法启动报销流程
            this.workFlowService.startBaoxiaoBill(baoxiaoBill.getId(),activeUser);
        }

        //最后返回一个重定向到taskList.jsp页面
        return "redirect:myTaskList";
    }

    /**待办事务中的办理任务*/
    @RequestMapping("/viewTaskForm")
    public String viewTaskForm(Model model,String taskId){
        //先获取任务ID获取对应的报销单对象
        BaoxiaoBill baoxiaoBill=this.workFlowService.findBaoxiaoBillByTaskId(taskId);
        //获取与该任务相关联的历史批注信息列表
        List<Comment>commentList=this.workFlowService.findCommentListByTaskId(taskId);

        List<String>outcomeList=this.workFlowService.findoutcomeListByTaskId(taskId);
        //将请假单信息添加到model对象中
        model.addAttribute("baoxiaoBill",baoxiaoBill);
        //任务id,taskId作为审批表单的隐藏域
        //将任务id添加到model对象中
        model.addAttribute("taskId",taskId);
        //将历史批注信息添加到model对象中
        model.addAttribute("commentList",commentList);
        //根据当前任务id查找当前活动完成后连接的所有连线名称列表
        model.addAttribute("outcomeList",outcomeList);
        //返回给前端页面
        return "approve_baoxiao";
    }

    /**结束任务*/
    @RequestMapping("/submitTask")
    public String submitTask(String taskId,String id,String comment,String outcome,HttpSession session){
        System.out.println("taskId:"+taskId);
        System.out.println("id:"+id);
        System.out.println("comment:"+comment);
        System.out.println("outcome:"+outcome);
        //完成任务
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();

        this.workFlowService.submitTaskId(taskId,id,comment,outcome,activeUser.getUsername());

        return "redirect:myTaskList";
    }

    /**查看当前流程图*/
    @RequestMapping("/viewCurrentImage")
    public String viewCurrentImage(String taskId,Model model){
        //通过任务ID查询流程定义对象
        ProcessDefinition pd=workFlowService.findProcessDefinitionByTaskId(taskId);
        model.addAttribute("deploymentId",pd.getDeploymentId());
        model.addAttribute("imageName",pd.getDiagramResourceName());
        //查看当前活动,将坐标4个值存到map中
        Map<String,Object> map=workFlowService.findCoordingByTask(taskId);
        model.addAttribute("acs",map);
        return "viewimage";
    }

    /**查看流程图*/
    @RequestMapping("/viewImage")
    public String viewImage(String deploymentId, String imageName, HttpServletResponse response) throws IOException {
        InputStream is=workFlowService.findImagInputStream(deploymentId,imageName);
        OutputStream os=response.getOutputStream();
        for (int b=-1;(b=is.read())!=-1;) {
            os.write(b);
        }
        os.close();
        is.close();
        return null;
    }
}
